# meson configuration for gperf
#
# Copyright (C) 2020 Tim-Philipp Müller <tim centricular com>
#
# This file is part of GNU GPERF.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

compare_output = find_program('compare_output.py')
extract_index_column = find_program('extract_index_column.py')

lang_utf8_foreign_name_gperf = custom_target('lang-utf8-foreign_name.gperf',
  input: 'lang-utf8.gperf',
  output: 'lang-utf8_foreign_name.gperf',
  command: [extract_index_column, '@INPUT@', '@OUTPUT@'])

# FIXME: quite a few tests missing, incl. all the validate ones

tests = {
  # Testing ANSI C reserved words, all items should be found in the set
  'c': ['cinset.c', 'c.gperf', ['-c', '-l', '-S1', '-I', '-o']],

  # Testing Ada reserved words, all items should be found in the set
  'ada-res': ['adainset.c', 'ada.gperf', ['-k1,4,$']],

  # Testing Ada predefined words, all items should be found in the set
  'ada-pred': ['adapreinset.c', 'adadefs.gperf', ['-D', '-k1,$', '-s', '2', '-o']],

  # Testing Modula3 reserved words, all items should be found in the set
  'modula': ['m3inset.c', 'modula3.gperf', ['-k1,2,$', '-o']],

  # Testing Pascal reserved words, all items should be found in the set
  'pascal': ['pinset.c', 'pascal.gperf', ['-o', '-S2']],

  # check for 8-bit cleanliness
  # Testing UTF-8 encoded languages, all items should be found in the set
  'lang-utf8': ['lu8inset.c', 'lang-utf8.gperf', ['-k1', '-t', '-K', 'foreign_name'], lang_utf8_foreign_name_gperf],
}

foreach tname, t : tests
  c_fn = t[0]
  gperf_fn = t[1]
  gperf_args = t[2]
  out_fn = '@0@.out'.format(tname)
  exp_fn = '@0@.exp'.format(tname)
  test_name = 'check-@0@'.format(tname)
  check_exe_input_fn = t.get(3, gperf_fn)

  gen_c_code = custom_target(c_fn,
    input: gperf_fn,
    output: c_fn,
    command: [gperf_exe, gperf_args, '-I', '@INPUT@'],
    capture: true)

  # run with exe_wrapper if cross compile?
  check_exe = executable('check-@0@'.format(tname), gen_c_code, 'test.c', native: true)

  gen_out = custom_target(out_fn,
    input: check_exe_input_fn,
    output: out_fn,
    command: [check_exe, '-v', '@INPUT@'],
    capture: true)

  test(test_name, compare_output, args: [files(exp_fn), gen_out])
endforeach
